package com.lizard.schedule.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lizard.schedule.R;

/**
 * 自定义view
 * 用于页面titlebar
 * 内容：左右两边图片按钮、标题
 */
public class Titlebar extends FrameLayout{

    private GestureDetector mGestureDetector;

    /**
     * 左边按钮
     */
    private ImageButton mLeftBtn;

    /**
     * 右边按钮
     */
    private ImageButton mRightBtn;

    /**
     * 标题
     */
    private TextView mTitle;

    /**
     * 双击事件
     */
    private OnDoubleClickListener mOnDoubleClickListener;

    public Titlebar(Context context) {
        this(context, null);
    }

    public Titlebar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Titlebar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setGestureDetector(context);
        initView(context, attrs);
    }

    /**
     * 设置标题
     *
     * @param titleResId
     */
    public void setTitle(int titleResId) {
        mTitle.setText(titleResId);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 手势设置
     */
    private void setGestureDetector(Context context){
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            public boolean onDown(MotionEvent e) {
                return true;
            }

            public boolean onDoubleTap(MotionEvent e) {
                if(mOnDoubleClickListener != null){
                    mOnDoubleClickListener.onDobuleClick();
                }
                return true;
            }
        });
    }


    /**
     * 右边按钮设置是否选中
     *
     * @param selected
     */
    public void setRightBtnSelected(boolean selected){
        mRightBtn.setSelected(selected);
    }

    /**
     * 左边按钮设置是否选中
     *
     * @param selected
     */
    public void setLeftBtnSelected(boolean selected){
        mLeftBtn.setSelected(selected);
    }

    /**
     * 左边按钮点击事件设置
     *
     * @param onClick
     */
    public void setLeftBtnClk(View.OnClickListener onClick){
        mLeftBtn.setOnClickListener(onClick);
    }

    /**
     * 右边按钮点击事件设置
     *
     * @param onClick
     */
    public void setRightBtnClk(View.OnClickListener onClick){
        mRightBtn.setOnClickListener(onClick);
    }

    private void initView(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.title_bar);

        mLeftBtn = (ImageButton) findViewById(R.id.left_btn);
        mRightBtn = (ImageButton) findViewById(R.id.right_btn);
        mTitle = (TextView) findViewById(R.id.title);

        int leftIconResId = typedArray.getResourceId(R.styleable.title_bar_left_icon, 0);
        int rightIconResId = typedArray.getResourceId(R.styleable.title_bar_right_icon, 0);
        int titleResId = typedArray.getResourceId(R.styleable.title_bar_title, 0);

        if(leftIconResId == 0){
            mLeftBtn.setVisibility(View.INVISIBLE);
        } else{
            mLeftBtn.setVisibility(View.VISIBLE);
            mLeftBtn.setImageResource(leftIconResId);
        }

        if(rightIconResId == 0){
            mRightBtn.setVisibility(View.INVISIBLE);
        } else{
            mRightBtn.setVisibility(View.VISIBLE);
            mRightBtn.setImageResource(rightIconResId);
        }

        if(titleResId == 0){
            mTitle.setVisibility(View.GONE);
        } else{
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(titleResId);
        }

        typedArray.recycle();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    /**
     * 双击事件设置
     *
     * @param onDoubleClickListener
     */
    public void setOnDoubleClickListener(OnDoubleClickListener onDoubleClickListener){
        mOnDoubleClickListener = onDoubleClickListener;
    }

    public interface OnDoubleClickListener{
        public void onDobuleClick();
    }
}
